//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: propagate.c 757 2007-05-10 08:19:31Z roger $
//

#include <glib.h>
#include <unihan_xaux.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <gtk/gtk.h>

#include "propagate.h"
#include "global.h"
#include "setting.h"

void
send_symbol_list_to_engine(GList *list)
{
	int int_values[1] = { UNIHAN_ENGINE_SET_SYMBOL_LIST };
	char **str_values = NULL;
	int len, i=0;
	int str_count = 0;
	GList *l = list;

	len = g_list_length(list);
	str_count = len * 3;
	if (str_count) {
		str_values = (char **)malloc(str_count *sizeof(char **) + 1);
		memset(str_values, 0, str_count * sizeof(char **) + 1);
	}

	for(i=0; l; i++, l=g_list_next(l)) {
		SymDefGroup *pSDG;
		pSDG = (SymDefGroup *)l->data;
		char buf[10];

		sprintf(buf, "%d", pSDG->modifier);
		str_values[i*3] = strdup(buf);

		sprintf(buf, "%d", pSDG->key);
		str_values[i*3 + 1] = strdup(buf);

		str_values[i*3 + 2] = pSDG->symbol;
	}

	send_to_engine(1, int_values, str_count, str_values);
	for(i=0; i<len; i++) {
		free(str_values[i*3]);
		free(str_values[i*3 + 1]);
	}
	if (str_values)
		free(str_values);
}

void
send_word_list_to_engine(GList *list)
{
	int int_values[] = { UNIHAN_ENGINE_SET_WORD_LIST };
	char **str_values = NULL;
	int len, i;
	int str_count = 0;
	GList *l = list;

	len = g_list_length(list);
	str_count = len * 2;
	if (str_count) {
		str_values = (char **)malloc(str_count * sizeof(char **) + 1);
		memset(str_values, 0, str_count * sizeof(char **) + 1);
	}

	for(i=0; l; i++, l=g_list_next(l)) {
		WordDefGroup *pWDG = (WordDefGroup *)l->data;
		str_values[i*2] = pWDG->key;
		str_values[i*2 + 1] = pWDG->word;
	}

	send_to_engine(1, int_values, str_count, str_values);

	if (str_values)
		free(str_values);
}

void
send_unispim_parameters_to_engine(int num_parameters,int parameters[])
{
	send_to_engine(num_parameters, parameters, 0, NULL);
}
#if NEW_PROPERTY
void send_props_to_engine(int num_props, UnihanPropertyArgList proplist)
{
	/*
	int *int_values;
	char **str_values = NULL;
	
	int_values = (int*)malloc(sizeof(int) * (num_props+1) + 1);
	str_values = (char **)malloc(sizeof(char *) * (num_props+1) + 1);
		
	memset(int_values, 0, sizeof(int) * (num_props+1) + 1);
	memset(str_values, 0, sizeof(char *) * (num_props+1) + 1);
	*/
	
	int int_values[21];
	char *str_values[21];
	int num_string;
	int i;
	
	int_values[0] = UNIHAN_ENGINE_IM_SET_PROPS;
	str_values[0] = "null";
	
	num_string = convert_props_to_string(proplist, num_props, int_values, str_values);
	
	send_to_engine(num_string, int_values, num_string, str_values);
	
	for(i = 1; i < num_string; i++)
		free((void*)str_values[i]);
		
}
#endif
#if NEW_PROPERTY
int
convert_props_to_string(UnihanPropertyArgList prop_list,
		int num_props,
		int int_values[],
		char* string_values[])
{
	int num_string = 1;
	char *buf;
	int i;
	size_t size;

	while(prop_list && num_props) {
		buf = (char*)malloc(sizeof(char)*1024);
		memset(buf, 0, sizeof(char)*1024);

		if(prop_list->type == UNIHAN_RPOPERTY_USER) {
			int_values[num_string] = UNIHAN_RPOPERTY_USER;

			size = prop_list->unihanProperty.u_property.size;

			if(size > 100) {
				buf[0] = size / 100;
				buf[1] = size % 100;
			} else {
				buf[0] = 0;
				buf[1] = size;
			}
			if(size)
				memcpy(buf+2, prop_list->unihanProperty.u_property.priv_data, size);
			for(i = 0; i < size+2; i++)
				if(buf[i] == 0)
					buf[i] = 127;
			string_values[num_string] = buf;

		} else if(prop_list->type == UNIHAN_PROPERTY_RUNTIME) {
			int_values[num_string] = UNIHAN_PROPERTY_RUNTIME;
			
			buf[0] = prop_list->unihanProperty.rt_property.rt_type+'0';
			buf[1] = prop_list->unihanProperty.rt_property.value+'0';
			
			if(prop_list->unihanProperty.rt_property.name.nr_name)
				buf[2] = prop_list->unihanProperty.rt_property.name.nr_name;
			else
				buf[2] = 127;
				
			if(prop_list->unihanProperty.rt_property.name.name_string)
				strcpy(buf+3, prop_list->unihanProperty.rt_property.name.name_string);
			else
				strcpy(buf+3, "null");
			strcat(buf,"|");
			
			if(prop_list->unihanProperty.rt_property.name.charset)
				strcat(buf, prop_list->unihanProperty.rt_property.name.charset);
			else
				strcat(buf, "null");
			strcat(buf,"|");
			
			if(prop_list->unihanProperty.rt_property.name.language)
				strcat(buf, prop_list->unihanProperty.rt_property.name.language);
			else
				strcat(buf, "null");
			strcat(buf,"|");

			if(prop_list->unihanProperty.rt_property.icon.icon0)
				strcat(buf, prop_list->unihanProperty.rt_property.icon.icon0);
			else
				strcat(buf, "null");
			strcat(buf,"|");

			if(prop_list->unihanProperty.rt_property.icon.icon1)
				strcat(buf, prop_list->unihanProperty.rt_property.icon.icon1);
			else
				strcat(buf, "null");

			string_values[num_string] = buf;
		}
		num_string++;
		num_props--;
		prop_list = prop_list->next;
	}

	return num_string;
}
#endif
#if NEW_PROPERTY
int convert_string_to_props(int int_values,unsigned char* string_values,UnihanPropertyArgList *pproplist)
{
	int i;
	size_t size;
	UnihanPropertyArgList new_property;
	
	if(string_values == NULL)
		return 0;
	
	new_property = (UnihanPropertyArgList)malloc(sizeof(UnihanPropertyArgs));
	
	if(int_values == UNIHAN_RPOPERTY_USER)
	{
		new_property->type = UNIHAN_RPOPERTY_USER;
			
		if(string_values[0] == 127)
			new_property->unihanProperty.u_property.size = size = string_values[1];
		else
			new_property->unihanProperty.u_property.size = size = string_values[0]*100+string_values[1];
			
		for(i = 2; i<size+2; i++)
			if(string_values[i] == 127)
				string_values[i] == 0;
			
		new_property->unihanProperty.u_property.priv_data = (void*)malloc(sizeof(char)*size);
		memcpy(new_property->unihanProperty.u_property.priv_data, (void*)(string_values+2), size);
		
	}else if(int_values == UNIHAN_PROPERTY_RUNTIME)
	{
		new_property->type = UNIHAN_PROPERTY_RUNTIME;
			
		new_property->unihanProperty.rt_property.rt_type = string_values[0]-'0';
			
		new_property->unihanProperty.rt_property.value = string_values[1]-'0';
			
		if(string_values[2] == 127)
			new_property->unihanProperty.rt_property.name.nr_name = 0;
		else
			new_property->unihanProperty.rt_property.name.nr_name = string_values[2];
			
		char *p;
		char *delim = "|";
		p = strtok(string_values + 3, delim);
			
		if(strcmp(p,"null")!=0)
		{
			new_property->unihanProperty.rt_property.name.name_string = (char*)malloc(sizeof(char)*(strlen(p)+1));
			strcpy(new_property->unihanProperty.rt_property.name.name_string, p);
		}else{
			new_property->unihanProperty.rt_property.name.name_string = NULL;
		}
			
		p = strtok(NULL , delim);
			
		if(strcmp(p,"null")!=0)
		{
			new_property->unihanProperty.rt_property.name.charset = (char*)malloc(sizeof(char)*(strlen(p)+1));
			strcpy(new_property->unihanProperty.rt_property.name.charset, p);
		}else{
			new_property->unihanProperty.rt_property.name.charset = NULL;
		}
		
		p = strtok(NULL , delim);
		
		if(strcmp(p,"null")!=0)
		{
			new_property->unihanProperty.rt_property.name.language = (char*)malloc(sizeof(char)*(strlen(p)+1));
			strcpy(new_property->unihanProperty.rt_property.name.language, p);
		}else{
			new_property->unihanProperty.rt_property.name.language = NULL;
		}
			 
		p = strtok(NULL , delim);
			
		if(strcmp(p,"null")!=0)
		{
			new_property->unihanProperty.rt_property.icon.icon0 = (char*)malloc(sizeof(char)*(strlen(p)+1));
			strcpy(new_property->unihanProperty.rt_property.icon.icon0, p);
		}else{
			new_property->unihanProperty.rt_property.icon.icon0 = NULL;
		}
			
		p = strtok(NULL , delim);
			
		if(strcmp(p,"null")!=0)
		{
			new_property->unihanProperty.rt_property.icon.icon1 = (char*)malloc(sizeof(char)*(strlen(p)+1));
			strcpy(new_property->unihanProperty.rt_property.icon.icon1, p);
		}else{
			new_property->unihanProperty.rt_property.icon.icon1 = NULL;
		}
	}
	
	if(*pproplist)
	{
		new_property->next = *pproplist;
	}else{
		new_property->next = NULL;
	}
	
	*pproplist = new_property;
			
	return 1;
		
}
#endif
#if NEW_PROPERTY
void destroy_im_property(UnihanPropertyArgList plist)
{
	UnihanPropertyArgList p;
	while(plist)
	{
		p = plist;
		plist = plist->next;
		if(p->type == UNIHAN_PROPERTY_RUNTIME)
		{
			if(p->unihanProperty.rt_property.name.name_string)
				free(p->unihanProperty.rt_property.name.name_string);
				
			if(p->unihanProperty.rt_property.name.charset)
				free(p->unihanProperty.rt_property.name.charset);
				
			if(p->unihanProperty.rt_property.name.language)
				free(p->unihanProperty.rt_property.name.language);
				
			if(p->unihanProperty.rt_property.icon.icon0)
				free(p->unihanProperty.rt_property.icon.icon0);
				
			if(p->unihanProperty.rt_property.icon.icon1)
				free(p->unihanProperty.rt_property.icon.icon1);
		}else if(p->type == UNIHAN_RPOPERTY_USER){
			free(p->unihanProperty.u_property.priv_data);
		}
		
		free(p);
	}
			
}
#endif

// vi:ts=4:nowrap:cin:expandtab
